package com.lepu.app.usercenter;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.usercenter.util.UserCenterUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

public class ModifyPwdActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest,OnEditorActionListener,OnTopbarNewRightButtonListener {

	private static final String REQUEST_OK = "request_ok";

	private static final int MSG_OK_SUCCESS = 10;
	private static final int MSG_OK_FAIL = 11;
	
	private String mPassword = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usercenter_modify_pwd_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarRightButtonSelected() {
		requestOk();
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		topbar.setRightText("提交");

		EditText pwdEditText2 = (EditText) findViewById(R.id.pwdEditText2);
		pwdEditText2.setOnEditorActionListener(this);
		
		ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				UtilityBase.closeSoftInput(ModifyPwdActivity.this);
				return false;
			}
		});
	}

	private void requestOk() {
		EditText oldPwdEditText = (EditText) findViewById(R.id.oldPwdEditText);
		EditText pwdEditText = (EditText) findViewById(R.id.pwdEditText);
		EditText pwdEditText2 = (EditText) findViewById(R.id.pwdEditText2);

		String oldPwdContent = oldPwdEditText.getText().toString().trim();
		String pwdContent = pwdEditText.getText().toString().trim();
		String pwdContent2 = pwdEditText2.getText().toString().trim();
		
		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
		boolean flag = UserCenterUtil.getModifyPwd(this, this, REQUEST_OK,
				loginInfo.UserID, loginInfo.LoginToken, oldPwdContent,
				pwdContent, pwdContent2);
		if (flag) {
			showProgressDialog();
			
			mPassword = pwdContent;
		}
	}

	View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.okButton:

				
				break;
			default:
				break;
			}
		}
	};

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_OK_SUCCESS:

				hideProgressDialog();
				
				//更新密码
				LoginUtil.savePassword(ModifyPwdActivity.this, mPassword);
				finish(true);
				break;
			case MSG_OK_FAIL:

				hideProgressDialog();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		if(object.equals(REQUEST_OK))
		{
			String result = (String) data;
			int code = 0;
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONObject obj1 = (JSONObject) jsonObject.get("Result");
				code = obj1.optInt("Status");
			} catch (JSONException e) {
				e.printStackTrace();
			}

			if (code == 200) {
				mHandler.sendEmptyMessage(MSG_OK_SUCCESS);
			} else {
				mHandler.sendEmptyMessage(MSG_OK_FAIL);
			}
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if(object.equals(REQUEST_OK))
		{
			mHandler.sendEmptyMessage(MSG_OK_FAIL);
		}
	}
	
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		switch (actionId) {
		case EditorInfo.IME_ACTION_DONE:
			
			requestOk();
			break;
		}
		return true;
	}

}
